home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 5 / FULLWRAP.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  149 lines

  1. // File referred to (not printed) on page 204 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: FULLWRAP.CPP -- Fully wrapped file IO
  34. #include "..\5\fullwrap.h"
  35. #include <assert.h>
  36. #include <stdlib.h>
  37. #include <stdarg.h>
  38.  
  39. File::File(){ f = 0; }
  40.  
  41. FILE* File::F() {
  42.   assert(f); // Or more sophisticated test
  43.   return f;
  44. }
  45.  
  46. File::File(const char* path, const char* mode){
  47.   open(path, mode);
  48. }
  49.  
  50. File::~File() {
  51.   if(f)
  52.     fclose(f);
  53. }
  54.  
  55. int File::open(const char* path,
  56.          const char* mode) {
  57.   if(f) fclose(f);
  58.   f = fopen(path, mode);
  59.   if(f == NULL) {
  60.     printf("%s: file not found", path);
  61.     exit(1);
  62.   }
  63.   return (int)f;
  64. }
  65.  
  66. int File::reopen(const char* path,
  67.            const char* mode) {
  68.   f = freopen(path, mode, f);
  69.   if(f == NULL) {
  70.     printf("%s: file not found", path);
  71.     exit(1);
  72.   }
  73.   return (int)f;
  74. }
  75.  
  76. int File::Getc() {
  77.   return fgetc(F());
  78. }
  79.  
  80. int File::Ungetc(int c) {
  81.   return ungetc(c, F());
  82. }
  83.  
  84. int File::Putc(int c) {
  85.   return fputc(c, F());
  86. }
  87.  
  88. int File::puts(const char* s) {
  89.   return fputs(s, F());
  90. }
  91.  
  92. char* File::gets(char* s, int n) {
  93.   return fgets(s, n, F());
  94. }
  95.  
  96. int File::printf(const char* format, ...) {
  97.    va_list argptr;
  98.    int cnt;
  99.    va_start(argptr, format);
  100.    cnt = vfprintf(F(), format, argptr);
  101.    va_end(argptr);
  102.    return cnt;
  103. }
  104.  
  105. size_t File::read(void* ptr, size_t size,
  106.              size_t n) {
  107.   return fread(ptr, size, n, F());
  108. }
  109.  
  110. size_t File::write(const void* ptr,
  111.               size_t size, size_t n) {
  112.   return fwrite(ptr, size, n, F());
  113. }
  114.  
  115. int File::eof() { return feof(F()); }
  116.  
  117. int File::close() { return fclose(F()); }
  118.  
  119. int File::flush() { return fflush(F()); }
  120.  
  121. int File::seek(long offset, int whence) {
  122.   return fseek(F(), offset, whence);
  123. }
  124.  
  125. int File::getpos(fpos_t* pos) {
  126.   return fgetpos(F(), pos);
  127. }
  128.  
  129. int File::setpos(const fpos_t* pos) {
  130.   return fsetpos(F(), pos);
  131. }
  132.  
  133. long File::tell() { return ftell(F()); }
  134.  
  135. void File::rewind() { ::rewind(F()); }
  136.  
  137. void File::setbuf(char* buf) {
  138.   ::setbuf(F(), buf);
  139. }
  140.  
  141. int File::setvbuf(char* buf, int type,
  142.                   size_t sz) {
  143.   return ::setvbuf(F(), buf, type, sz);
  144. }
  145.  
  146. int File::error() { return ferror(F()); }
  147.  
  148. void File::Clearerr() { clearerr(F()); }
  149.